﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "MultipleBurnDlgForOne.h"
#include <QMessageBox>
#include <QDir>
#include "BurnCtrl/BurnCtrlFaction.h"

using namespace BurnDlg;

namespace {
    const int DEFAULT_PERIOD = 10;
    const int DEFAULT_SLEEP_TIMEOUT = 100;
    const int DEFAULT_WAIT_TIMEOUT = 200;
    const int PACKET_SIZE = 1024;
}

MultipleBurnDlgForOne::MultipleBurnDlgForOne(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::MultipleBurnDlgForOne),
      burnCtrl(nullptr),
      chipType(ChipType::WIFI5GNB),
      isAuto(true),
      isReopenCom(false),
      bootType(BOOT_ROM),
      isEraseMode(0),
      isSecConn(false),
      period(DEFAULT_PERIOD),
      baud(0),
      databits(0),
      stopbits(0),
      parity(QSerialPort::NoParity),
      flowctrl(0),
      forceReadTime(0),
      isForceReadWithSleep(false),
      connectConfirmSleepTimeout(DEFAULT_SLEEP_TIMEOUT),
      connectConfirmWaitTimeout(DEFAULT_WAIT_TIMEOUT),
      indenpendent(false),
      strFilePath(""),
      isResultByOwner(false),
      sendBurnTimeout(0),
      isExactTimer(false),
      isResetAfterSuccess(false),
      modemPacketSizeBackup(PACKET_SIZE),
      isSwitchAfterLoader(false),
      isEncrypt(false),
      transmissionMode(0),
      chipModel(0),
      infoStartAddress(0),
      isReadBack(false),
      isRun(false),
      loaderFile(""),
      loaderSize(0),
      parameterFilePath(""),
      eraseBaseNum(0)
{
    if (ui == nullptr) {
        return;
    }
    ui->setupUi(this);
    ui->progressBar->setRange(0, SHRT_MAX);
    ui->progressBar->setValue(0);
}

MultipleBurnDlgForOne::~MultipleBurnDlgForOne()
{
    if (burnCtrl != nullptr) {
        delete burnCtrl;
        burnCtrl = nullptr;
    }
    if (ui != nullptr) {
        delete ui;
        ui = nullptr;
    }
}

int MultipleBurnDlgForOne::GetSelectCom() const
{
    if (ui == nullptr) {
        return 0;
    }
    QString strPort = ui->comCombo->currentText();
    return strPort.mid(3).toInt(); // 获取第3字节开始的串口号
}

HI_BURN_FSM MultipleBurnDlgForOne::GetBurnFsm() const
{
    if (burnCtrl != nullptr) {
        return burnCtrl->GetBurnFsm();
    }
    return HI_BURN_FSM::FSM_NONE;
}

void MultipleBurnDlgForOne::InitAllCom()
{
    BurnCtrlFaction faction;
    burnCtrl = faction.GetBurnCtrl(chipType);
    if (ui == nullptr || burnCtrl == nullptr) {
        return;
    }
    burnCtrl->StartWiteMonitoring();
    connect(this, &MultipleBurnDlgForOne::SignalSendMsg, burnCtrl->GetBurnCtrlWorker(),
            &BurnCtrlWorkerClass::BurnCtrlWorker::SlotSendMsg);
    connect(burnCtrl, &BurnCtrl::SignalInsertPlainText, this, &MultipleBurnDlgForOne::SlotInsertPlainText);
    connect(burnCtrl, &BurnCtrl::SignalNotifyState, this, &MultipleBurnDlgForOne::SignalNotifyState);
    connect(burnCtrl, &BurnCtrl::SignalMergeCurrentCharFormat, this,
            &MultipleBurnDlgForOne::SlotMergeCurrentCharFormat);
    connect(burnCtrl, &BurnCtrl::SignalSetProgressValue, this, &MultipleBurnDlgForOne::SlotSetProgressValue);
    connect(burnCtrl, &BurnCtrl::SignalOpenComFail, this, &MultipleBurnDlgForOne::SlotOpenComFail);
    connect(ui->comCombo, &ComboBoxCom::Clicked, this, &MultipleBurnDlgForOne::SlotCbnSetfocusComboCom);
}


void MultipleBurnDlgForOne::SlotShowLog(QString logStr, bool isShowTime, QColor color) const
{
    if (logStr == "") {
        return;
    }
    QDateTime time = QDateTime::currentDateTime();
    QString timeTip = time.toString("yyyy-MM-dd hh:mm:ss");
    QString log = logStr;
    static bool isEnter = false;
    if (logStr == "#") {
        if (!isEnter) {
            isEnter = true;
            log = '\n' + logStr;
        } else {
            log = logStr;
        }
    } else {
        if (isEnter) {
            isEnter = false;
            if (isShowTime) {
                log = '\n' + timeTip + ": "+ logStr;
            } else {
                log = '\n' + logStr;
            }
        }
    }

    ui->textEdit->moveCursor(QTextCursor::End);
    QTextCharFormat fmt;
    fmt.setForeground(QBrush(color));
    if (log.toLower().contains("error")) {
        fmt.setForeground(QBrush(Qt::red));
    }
    ui->textEdit->mergeCurrentCharFormat(fmt);
    ui->textEdit->insertPlainText(log);
}

void MultipleBurnDlgForOne::SlotSetProgressValue(unsigned int progress) const
{
    if (ui == nullptr) {
        return;
    }
    ui->progressBar->setValue(progress);
}

void MultipleBurnDlgForOne::FindAndSelectString(QString target) const
{
    if (ui == nullptr) {
        return;
    }
    int res = ui->comCombo->findText(target);
    ui->comCombo->setCurrentIndex(res);
}

U32 MultipleBurnDlgForOne::OnlyConnectHandle()
{
    UpdateCmdIfIdle();
    if (burnCtrl == nullptr || ui == nullptr) {
        return ERR_SOP_FAILURE;
    }
    // 关闭串口
    if (burnCtrl->IsComOpen()) {
        return ERR_SOP_SUCCESS;
    }
    if (chipType != ChipType::SPARTA && chipType != ChipType::COMBS25 && chipType != ChipType::MELODY) {
        burnCtrl->UpdateComSetting(OPEN_SERIAL_PORT_DEFAULT_BAUD_RATE, OPEN_SERIAL_PORT_DEFAULT_DATA_BIT,
                                   OPEN_SERIAL_PORT_DEFAULT_STOP_BIT, QSerialPort::NoParity,
                                   OPEN_SERIAL_PORT_DEFAULT_FLOWCTRL);
    }
    // 打开串口，并启动连接流程
    if (!ConfigComPort()) {
        ui->textEdit->clear();
        return ERR_SOP_FAILURE;
    }
    ui->textEdit->clear();
    ui->textEdit->insertPlainText(stringClass.GetIdsString32897());
    if (chipType == ChipType::SPARTA || chipType == ChipType::MELODY || chipType == ChipType::COMBS25) {
        burnCtrl->SetComNum(ui->comCombo->currentText().mid(3).toInt()); // 第3位为串口号
        burnCtrl->InitBurnCmdByArray(binInfoArray);
        ui->comCombo->setEnabled(false);
    } else {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_INITBURNCMD),
                           static_cast<WPARAM>(0),
                           static_cast<LPARAM>(0));
    }
    emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_STARTCONNECT),
                       static_cast<WPARAM>(0),
                       static_cast<LPARAM>(0));
    emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_SETRESULT),
                       static_cast<WPARAM>(ERR_WAITING),
                       static_cast<LPARAM>(sizeof(ERR_WAITING)));

    return ERR_SOP_SUCCESS;
}

U32 MultipleBurnDlgForOne::ConnectUSBHandle()
{
    if (this->chipType != ChipType::USB4GCAT1 && chipType != ChipType::USBHI3322) {
        return ERR_SOP_FAILURE;
    }

    if (burnCtrl == nullptr) {
        return ERR_SOP_FAILURE;
    }

    burnCtrl->SetAutoBurnFlag(true);
    burnCtrl->SetLoaderDfu(true);

    ui->textEdit->clear();
    ui->textEdit->insertPlainText(stringClass.GetIdsString32897());

    this->deviceInfo.locationInfo = ui->comCombo->currentText();
    burnCtrl->SetUsbDeviceInfo(this->deviceInfo);

    burnCtrl->InitBurnCmdByArray(binInfoArray);

    emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_SETRESULT), static_cast<WPARAM>(ERR_WAITING),
                       static_cast<LPARAM>(sizeof(ERR_WAITING)));
    emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_STARTCONNECT), 0, 0);
    return ERR_SOP_SUCCESS;
}

U32 MultipleBurnDlgForOne::ConnectMCUHandle()
{
    if (burnCtrl == nullptr) {
        return ERR_SOP_FAILURE;
    }

    mcuConfig.portName = ui->comCombo->currentText();
    burnCtrl->SetMcuConfig(mcuConfig);
    burnCtrl->SetBurnPath(strFilePath);
    burnCtrl->InitBurnCmdByArray(binInfoArray);
    emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_STARTCONNECT), 0, 0);
    return ERR_SOP_SUCCESS;
}

void MultipleBurnDlgForOne::UpdateCmdIfIdle()
{
    if (burnCtrl != nullptr && burnCtrl->IsComOpen()) {
        return;
    }
    emit SignalUpdateList();
}

bool MultipleBurnDlgForOne::ConfigComPort() const
{
    if (ui == nullptr) {
        return false;
    }
    QString strPort = ui->comCombo->currentText();
    QStringList result;
    DataConvertor::RefreshComList(result);
    boolean inList = false;
    for (int i = 0; i < result.size(); i++) {
        if (strPort == result[i]) {
            inList = true;
            break;
        }
    }
    if (inList == false) {
        return false;
    }
    int ret = ui->comCombo->findText(strPort);
    if (ret != -1) {
        ui->comCombo->setCurrentText(strPort);
    }
    if (-1 == ui->comCombo->currentText()) {
        return false;
    } else {
        int com = strPort.mid(3).toInt(); // 获取第3字节开始的串口号
        if (burnCtrl != nullptr) {
            burnCtrl->SetComNum(com);
            burnCtrl->ConfigComPort();
        }
    }
    return true;
}

void MultipleBurnDlgForOne::RefreshAllComList(QStringList &array, int index) const
{
    if (ui == nullptr) {
        return;
    }
    for (int i = 0; i < array.size(); i++) {
        ui->comCombo->addItem(array[i]);
    }
    ui->comCombo->setCurrentIndex(index);
}

void MultipleBurnDlgForOne::InitAllConfig()
{
    InitAllCom();
    SetAutoBurnFlag();
    if (this->chipType == ChipType::USB4GCAT1 || chipType == ChipType::USBHI3322) {
        if (burnCtrl != nullptr) {
            burnCtrl->SetIsAutoDfu(DfuBurnMode::AUTO_DFU);
        }
    }

    SetReopenComFlag();
    SetInfoArray();
    SetSecConn();
    SetPeriod();
    UpdateComSetting();
    SetForceReadFlag();
    SetForceReadTime();
    SetIsResetAfterSuccess();
    SetYModemEachPackageSize();
    SetIsSwitchAfterLoader();
    SetBootType();
    SetResultByOwnerFlag();
    SetBurnTimeout();
    SetConfirmConnectSleepTime();
    SetConfirmConnectWaitTime();
    SetEraseAll();
    SetUseExactTimer();
    SetIndenpendentBurn();
    SetEncryptBuffer();
    SetIsEncrypt();
    RegisteAllEdit();
    SetMcuConfig();
    SetChipModel();
    SetJtagSwd();
    SetTransmissionMode();
    SetChipId();
    SetInfoStartAddress();
    SetMainStartAddress();
    SetSramAddrInfo();
    SetIsReadBack();
    SetIsRun();
    SetLoaderInfo();
    SetParameterSaveInfo();

    SetBurnFileName();
}

void MultipleBurnDlgForOne::SetChipType(ChipType type)
{
    this->chipType = type;
}

void MultipleBurnDlgForOne::SetAutoBurnFlag(bool isAutoNeedSet)
{
    this->isAuto = isAutoNeedSet;
}

void MultipleBurnDlgForOne::SetBootType(BootType type)
{
    this->bootType = type;
}

void MultipleBurnDlgForOne::SetReopenComFlag(bool isReopenComNeedSet)
{
    this->isReopenCom = isReopenComNeedSet;
}

void MultipleBurnDlgForOne::SetPeriod(int periodNeedSet)
{
    this->period = periodNeedSet;
}

void MultipleBurnDlgForOne::SetInfoArray(const QMap<int, BIN_INFO> &infoArray)
{
    this->binInfoArray = infoArray;
}

void MultipleBurnDlgForOne::SetInfoArray(const QString &filePath)
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetFileInfo(filePath);
    }
    strFilePath = filePath;
}

void MultipleBurnDlgForOne::SetSecConn(bool isSecConnNeedSet)
{
    this->isSecConn = isSecConnNeedSet;
}

void MultipleBurnDlgForOne::SetEraseAll(int eraseMode)
{
    this->isEraseMode = eraseMode;
}

void MultipleBurnDlgForOne::UpdateComSetting(UINT baudNeedSet, UINT databitsNeedSet, UINT stopbitsNeedSet,
                                             QSerialPort::Parity parityNeedSet, UINT flowctrlNeedSet)
{
    this->baud = baudNeedSet;
    this->databits = databitsNeedSet;
    this->stopbits = stopbitsNeedSet;
    this->parity = parityNeedSet;
    this->flowctrl = flowctrlNeedSet;
}

void MultipleBurnDlgForOne::SetForceReadFlag(bool isForceRead)
{
    this->isForceReadWithSleep = isForceRead;
}

void MultipleBurnDlgForOne::SetForceReadTime(UINT forceReadTimeNeedSet)
{
    this->forceReadTime = forceReadTimeNeedSet;
}

void MultipleBurnDlgForOne::SetIsResetAfterSuccess(bool isResetAfterSuccessNeedSet)
{
    this->isResetAfterSuccess = isResetAfterSuccessNeedSet;
}

void MultipleBurnDlgForOne::SetYModemEachPackageSize(unsigned int modemPacketSizeBackupNeedSet)
{
    this->modemPacketSizeBackup = modemPacketSizeBackupNeedSet;
}

void MultipleBurnDlgForOne::SetIsSwitchAfterLoader(bool isSwitchAfterLoaderNeedSet)
{
    this->isSwitchAfterLoader = isSwitchAfterLoaderNeedSet;
}

void MultipleBurnDlgForOne::SetConfirmConnectSleepTime(UINT time)
{
    this->connectConfirmSleepTimeout = time;
}

void MultipleBurnDlgForOne::SetConfirmConnectWaitTime(UINT time)
{
    this->connectConfirmWaitTimeout = time;
}

void MultipleBurnDlgForOne::SetResultByOwnerFlag(bool isResultByOwnerNeedSet)
{
    this->isResultByOwner = isResultByOwnerNeedSet;
}

void MultipleBurnDlgForOne::SetBurnTimeout(UINT timeout)
{
    this->sendBurnTimeout = timeout;
}

void MultipleBurnDlgForOne::SetUseExactTimer(bool useExactTimer)
{
    this->isExactTimer = useExactTimer;
}

void MultipleBurnDlgForOne::SetIndenpendentBurn(bool indenpendentNeedSet)
{
    this->indenpendent = indenpendentNeedSet;
}

void MultipleBurnDlgForOne::SetIsEncrypt(bool isEncryptNeedSet)
{
    this->isEncrypt = isEncryptNeedSet;
}

void MultipleBurnDlgForOne::SetEncryptBuffer(ENCRYPT_BUFFER encryptBufferNeedSet)
{
    this->encryptBuffer = encryptBufferNeedSet;
}

void MultipleBurnDlgForOne::SetAutoBurnFlag() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetAutoBurnFlag(isAuto);
    }
}

void MultipleBurnDlgForOne::SetReopenComFlag() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetReopenComFlag(isReopenCom);
    }
}

void MultipleBurnDlgForOne::SetInfoArray()
{
    if (burnCtrl == nullptr) {
        return;
    }
    burnCtrl->InitBurnCmdByArray(binInfoArray);
}

void MultipleBurnDlgForOne::SetSecConn() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetSecConn(isSecConn);
    }
}

void MultipleBurnDlgForOne::SetPeriod() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetPeriod(period);
    }
}

void MultipleBurnDlgForOne::UpdateComSetting() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->UpdateComSetting(baud, databits, stopbits, parity, flowctrl);
    }
}

void MultipleBurnDlgForOne::SetForceReadFlag() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetForceReadFlag(isForceReadWithSleep);
    }
}

void MultipleBurnDlgForOne::SetForceReadTime() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetForceReadTime(forceReadTime);
    }
}

void MultipleBurnDlgForOne::SetIsResetAfterSuccess() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetIsResetAfterSuccess(isResetAfterSuccess);
    }
}

void MultipleBurnDlgForOne::SetYModemEachPackageSize() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetYModemEachPackageSize(modemPacketSizeBackup);
    }
}

void MultipleBurnDlgForOne::SetIsSwitchAfterLoader() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetIsSwitchAfterLoader(isSwitchAfterLoader);
    }
}

void MultipleBurnDlgForOne::SetBootType() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SwitchBoot(bootType);
    }
}

void MultipleBurnDlgForOne::SetResultByOwnerFlag() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetResultByOwnerFlag(isResultByOwner);
    }
}

void MultipleBurnDlgForOne::SetBurnTimeout() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetBurnTimeout(sendBurnTimeout);
    }
}

void MultipleBurnDlgForOne::SetConfirmConnectSleepTime() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetConfirmConnectSleepTime(connectConfirmSleepTimeout);
    }
}

void MultipleBurnDlgForOne::SetConfirmConnectWaitTime() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetConfirmConnectWaitTime(connectConfirmSleepTimeout);
    }
}

void MultipleBurnDlgForOne::SetEraseAll() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetEraseMode(isEraseMode);
    }
}

void MultipleBurnDlgForOne::SetUseExactTimer() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetUseExactTimer(isExactTimer);
    }
}

void MultipleBurnDlgForOne::SetIndenpendentBurn() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetMultiBurnIndenpendentBurn(indenpendent);
    }
}

void MultipleBurnDlgForOne::RegisteAllEdit() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->RegisteResultEdit(true);
    }
}

void MultipleBurnDlgForOne::SetIsEncrypt() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetIsEncrypt(isEncrypt);
    }
}

void MultipleBurnDlgForOne::SetEncryptBuffer()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetEncryptBuffer(encryptBuffer.bufferData);
        burnCtrl->SetEncryptBufferLen(encryptBuffer.bufferLen);
    }
}

void MultipleBurnDlgForOne::SetMcuConfig()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetMcuConfig(mcuConfig);
    }
}

void MultipleBurnDlgForOne::SetChipModel() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetChipModel(chipModel);
    }
}

void MultipleBurnDlgForOne::SetJtagSwd()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetJtagSwd(jtagSwd);
    }
}

void MultipleBurnDlgForOne::SetTransmissionMode() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetTransmissionMode(transmissionMode);
    }
}

void MultipleBurnDlgForOne::SetChipId()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetChipId(chipId);
    }
}

void MultipleBurnDlgForOne::SetInfoStartAddress() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetInfoStartAddress(infoStartAddress);
    }
}

void MultipleBurnDlgForOne::SetMainStartAddress()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetMainStartAddress(mainStartAddrInfo);
    }
}

void MultipleBurnDlgForOne::SetSramAddrInfo()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetSramAddrInfo(sramAddrInfo);
    }
}

void MultipleBurnDlgForOne::SetIsReadBack() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetIsReadBack(isReadBack);
    }
}

void MultipleBurnDlgForOne::SetIsRun() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetIsRun(isRun);
    }
}

void MultipleBurnDlgForOne::SetLoaderInfo()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetLoaderInfo(loaderFile, loaderSize);
    };
}

void MultipleBurnDlgForOne::SetParameterSaveInfo()
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetParameterSaveInfo(parameterFilePath, eraseBaseNum);
    };
}

void MultipleBurnDlgForOne::SlotInsertPlainText(const QString &editStr) const
{
    if (ui == nullptr) {
        return;
    }
    ui->textEdit->clear();
    ui->textEdit->insertPlainText(editStr);
    QPalette pal = ui->textEdit->palette();
    if (editStr == tr("Pass")) {
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_GREEN);
        ui->textEdit->setPalette(pal);
    } else if (editStr == tr("Doing")) {
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_YELLOW);
        ui->textEdit->setPalette(pal);
    } else if (editStr == tr("Waiting")) {
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setPalette(pal);
    } else if (editStr == tr("Fail")) {
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_RED);
        ui->textEdit->setPalette(pal);
    }
}

void MultipleBurnDlgForOne::ShowResult()
{
    if (burnCtrl != nullptr && burnCtrl->IsComOpen()) {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_SHOWRESULT),
                           static_cast<WPARAM>(0),
                           static_cast<LPARAM>(0));
    }
}

void MultipleBurnDlgForOne::SetResultFromAToB(unsigned int resultA, unsigned int resultB)
{
    if (burnCtrl != nullptr && burnCtrl->IsComOpen()) {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_SETRESULT_A2B),
                           static_cast<WPARAM>(resultA),
                           static_cast<LPARAM>(resultB));
    }
}

unsigned int MultipleBurnDlgForOne::GetResult() const
{
    if (burnCtrl != nullptr) {
        return burnCtrl->GetResult();
    }
    return 0;
}

void MultipleBurnDlgForOne::SetResult(unsigned int result)
{
    if (burnCtrl != nullptr && burnCtrl->IsComOpen()) {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_SETRESULT),
                           static_cast<WPARAM>(result),
                           sizeof(result));
    }
}

void MultipleBurnDlgForOne::SlotMergeCurrentCharFormat(const QTextCharFormat &fmt) const
{
    if (ui == nullptr) {
        return;
    }
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MultipleBurnDlgForOne::KillAllTimer()
{
    if (burnCtrl != nullptr) {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_KILLALLTIMER), static_cast<WPARAM>(0), 0);
    }
}

void MultipleBurnDlgForOne::SetShutDown() const
{
    if (burnCtrl != nullptr) {
        burnCtrl->SetShutDown();
    }
}

U32 MultipleBurnDlgForOne::OnlyDisconnectHandle()
{
    if (ui == nullptr) {
        return -1;
    }
    // 关闭串口
    if (burnCtrl != nullptr && burnCtrl->IsComOpen()) {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_RESETUIANDFSM), static_cast<WPARAM>(0), 0);
        QPalette pal = ui->textEdit->palette();
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setPalette(pal);
        ui->textEdit->setTextColor(COLOR_RGB_VALUE_BLACK);
        ui->textEdit->setTextBackgroundColor(COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setText("Stop");
        burnCtrl->SetIsCancel(TRUE);
        burnCtrl->BurnCtrlCancel();
        burnCtrl->SetIsFirstBurnStart(true);
        ui->comCombo->setEnabled(true);
    }
    return ERR_SOP_SUCCESS;
}

U32 MultipleBurnDlgForOne::DisconnectUSBHandle()
{
    if (ui == nullptr) {
        return -1;
    }

    // 关闭USB
    if (burnCtrl != nullptr && (this->chipType == ChipType::USB4GCAT1 || chipType == ChipType::USBHI3322)) {
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_RESETUIANDFSM), static_cast<WPARAM>(0), 0);
        QPalette pal = ui->textEdit->palette();
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setPalette(pal);
        ui->textEdit->setTextColor(COLOR_RGB_VALUE_BLACK);
        ui->textEdit->setTextBackgroundColor(COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setText("Stop");
        burnCtrl->SetIsCancel(TRUE);
        burnCtrl->BurnCtrlCancel();
        burnCtrl->SetIsFirstBurnStart(true);
        ui->comCombo->setEnabled(true);
    }
    return ERR_SOP_SUCCESS;
}

U32 MultipleBurnDlgForOne::DisconnectMCUHandle()
{
    if (ui == nullptr) {
        return -1;
    }
    if (ui->comCombo->currentText().isEmpty()) {
        return -1;
    }

    // 关闭USB
    if (burnCtrl != nullptr) {
        QPalette pal = ui->textEdit->palette();
        pal.setBrush(QPalette::Base, COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setPalette(pal);
        ui->textEdit->setTextColor(COLOR_RGB_VALUE_BLACK);
        ui->textEdit->setTextBackgroundColor(COLOR_RGB_VALUE_WHITE);
        ui->textEdit->setText("Stop");
        burnCtrl->SetIsCancel(true);
        burnCtrl->BurnCtrlCancel();
        emit SignalSendMsg(static_cast<long>(WiteThreadMsg::WITE_THREAD_MSG_RESETUIANDFSM), 0, 0);
    }
    return ERR_SOP_SUCCESS;
}

void MultipleBurnDlgForOne::SlotOpenComFail()
{
    if (ui == nullptr) {
        return;
    }
    ui->comCombo->setEnabled(true);
    QMessageBox::warning(nullptr, "",
                         stringClass.GetIdsString32812().arg(ui->comCombo->currentText().mid(3))); // 第3位为串口号
}

void MultipleBurnDlgForOne::SlotCbnSetfocusComboCom()
{
    if (ui == nullptr) {
        return;
    }

    if (!Global::IsUSBType(this->chipType)) {
        QString strPort = ui->comCombo->currentText();
        ui->comCombo->clear();

        QStringList strListPort;
        DataConvertor::RefreshComList(strListPort);
        ui->comCombo->addItems(strListPort);
        for (int i = 0; i < strListPort.size(); i++) {
            if (strListPort.at(i) == strPort) {
                ui->comCombo->setCurrentText(strPort);
            }
        }
    } else if (this->chipType == ChipType::USB4GCAT1) {
        this->deviceInfo.locationInfo = ui->comCombo->currentText();
    }
}

void MultipleBurnDlgForOne::SetBurnFileName()
{
    if (burnCtrl == nullptr) {
        return;
    }
    burnCtrl->SetBurnFileName(strFilePath);
}

void MultipleBurnDlgForOne::SetUsbDeviceInfo(HidDeviceInfo usbDeviceInfo)
{
    this->deviceInfo = usbDeviceInfo;
}

void MultipleBurnDlgForOne::SetChipModel(int chipModelNeedSet)
{
    this->chipModel = chipModelNeedSet;
}

void MultipleBurnDlgForOne::SetMcuConfig(McuConfig config)
{
    this->mcuConfig = config;
}

void MultipleBurnDlgForOne::SetJtagSwd(McuJtagSwd config)
{
    this->jtagSwd = config;
}

void MultipleBurnDlgForOne::SetTransmissionMode(int tarns)
{
    this->transmissionMode = tarns;
}

void MultipleBurnDlgForOne::SetChipId(McuChipId id)
{
    this->chipId = id;
}


void MultipleBurnDlgForOne::SetInfoStartAddress(ulong startAddr)
{
    this->infoStartAddress = startAddr;
}

void MultipleBurnDlgForOne::SetMainStartAddress(MainStartAddrInfo startAddrInfo)
{
    this->mainStartAddrInfo = startAddrInfo;
}

void MultipleBurnDlgForOne::SetSramAddrInfo(SramAddrInfo sainfo)
{
    this->sramAddrInfo = sainfo;
}

void MultipleBurnDlgForOne::SetIsReadBack(bool readBack)
{
    this->isReadBack = readBack;
}

void MultipleBurnDlgForOne::SetIsRun(bool run)
{
    this->isRun = run;
}

void MultipleBurnDlgForOne::SetLoaderInfo(QString loaderFileNeedSet, int loaderSizeNeedSet)
{
    this->loaderFile = loaderFileNeedSet;
    this->loaderSize = loaderSizeNeedSet;
}

void MultipleBurnDlgForOne::SetParameterSaveInfo(const QString &filePath, ulong baseNum)
{
    this->parameterFilePath = filePath;
    this->eraseBaseNum = baseNum;
}

